﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour
{
    public Animator anim;

    [Tooltip("关卡切换的动画时间")]
    public float time = 0.5f;

    [Tooltip("如果关卡是最后一关时，需要勾选该选项")]
    public bool isGameOver = false;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            SwitchLevel();
        }
    }

    public void SwitchLevel()
    {
        if (!isGameOver)
        {
            StartCoroutine(Load_Level(SceneManager.GetActiveScene().buildIndex + 1));
        }
        else
        {
            StartCoroutine(Load_Level(0));
        }
    }

    IEnumerator Load_Level(int level_num)
    {
        // anim
        anim.SetTrigger("start");

        // wait
        yield return new WaitForSeconds(time);

        // switch level
        SceneManager.LoadScene(level_num);
    }
}
